package cn.com.cifi.mars.service.impl;

import cn.com.cifi.mars.bean.vo.price.FilePreviewVO;
import cn.com.cifi.mars.constant.FileTypeEnum;
import cn.com.cifi.mars.constant.IsDeleteEnum;
import cn.com.cifi.mars.entity.PriceVersion;
import cn.com.cifi.mars.entity.UploadFile;
import cn.com.cifi.mars.mapper.mars.ApprovedProjectMapper;
import cn.com.cifi.mars.mapper.mars.FileUploadMapper;
import cn.com.cifi.mars.service.FileUploadService;
import cn.com.cifi.mars.util.UUIDUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class FileUploadServiceImpl extends ServiceImpl<FileUploadMapper,UploadFile> implements FileUploadService {
    @Autowired
    FileUploadMapper fileUploadMapper;
    @Override
    public List<UploadFile> selectSameFileName(String fileName) {
        QueryWrapper<UploadFile> fileQueryWrapper = new QueryWrapper<UploadFile>();
        fileQueryWrapper.eq("file_name",fileName);
        List<UploadFile> uploadFile = fileUploadMapper.selectList(fileQueryWrapper);
        return uploadFile;
    }

    @Override
    public String saveFileUploadRecord(String fileName, String suffixName,String address,String originalFilename,String createdBy) {
        UploadFile uploadFile = new UploadFile();
        String type = suffixName.substring(1);
        String id = UUIDUtils.create();
        uploadFile.setId(id);
        uploadFile.setFileName(fileName);
        uploadFile.setOriginalFileName(originalFilename);
        uploadFile.setFileType(type);
        uploadFile.setFileStoreAddress(address);
        uploadFile.setUploadTime(LocalDateTime.now());
        uploadFile.setType(FileTypeEnum.VALUE_APPROVEL.getKey());
        uploadFile.setCreatedBy(createdBy);
        uploadFile.setIsDelete(IsDeleteEnum.NO.getKey());
        fileUploadMapper.saveFileUploadRecord(uploadFile);
        return id;
    }

    @Override
    public UploadFile selectFileById(String fileId) {
        return fileUploadMapper.selectFileById(fileId);
    }

    @Override
    public void updateBillId(String businessKey, List<String> files) {
        fileUploadMapper.updateBillId(businessKey,files);
    }

    @Override
    public void updatePreViewUrl(String id,String url) {
        fileUploadMapper.updatePreViewUrl(id,url);
    }

    @Override
    public List<UploadFile> selectFileByBusinessKey(String businessKey) {
        QueryWrapper<UploadFile> fileQueryWrapper = new QueryWrapper<UploadFile>();
        fileQueryWrapper.eq("relation_id",businessKey).eq("is_delete",IsDeleteEnum.NO.getKey());
        List<UploadFile> list = fileUploadMapper.selectList(fileQueryWrapper);
        return list;
    }

    @Override
    public int deleteFile(String fileId, int del) {
        return fileUploadMapper.updateIsDelete(fileId,del);
    }


    /**
     * 本次编制的版本
     * 查询 审批的附件
     * @param billId
     * @return
     */
    @Override
    public List<FilePreviewVO> getFilePreview(String billId) {
        List<FilePreviewVO> list = new ArrayList<>();
        List<UploadFile> fileList = fileUploadMapper.selectByRelationId(billId,IsDeleteEnum.NO.getKey());
        for (UploadFile uploadFile : fileList) {
            FilePreviewVO previewVO = new FilePreviewVO();
            previewVO.setFileId(uploadFile.getId());
            previewVO.setFileName(uploadFile.getOriginalFileName());
            previewVO.setFilePath(uploadFile.getFileStoreAddress());
            previewVO.setPreviewUrl(uploadFile.getPreViewUrl());
            list.add(previewVO);
        }
        return list;
    }
}
